﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS.cntr;
using System.Collections;
using SMS.util;
using System.Resources;

namespace SMS.view
{
    public partial class CreateStudentForm : Form
    {
        private int studentId;

        public CreateStudentForm()
        {
            InitializeComponent();
        }

        private void CreateStudentForm_Load(object sender, EventArgs e)
        {
            bindGridViewStudents("");
            doAutoCompleteForTxtSearch();
            changeLang(I18nUtil.vnLang);
        }

        public void changeLang(String lang)
        {
            I18nUtil.init(lang, new ResourceManager(I18nUtil.resourcePath, typeof(CreateStudentForm).Assembly));

            this.Text = I18nUtil.getString("createStudent");
            label1.Text = I18nUtil.getString("fullName");
            label2.Text = I18nUtil.getString("DTB");
            label3.Text = I18nUtil.getString("email");
            label4.Text = I18nUtil.getString("gender");
            label5.Text = I18nUtil.getString("address");
            createStudentBtn.Text = I18nUtil.getString("btnCreate");
            btnUpdate.Text = I18nUtil.getString("btnUpdate");
            txtSearch.Text = I18nUtil.getString("search");
            groupBox1.Text = I18nUtil.getString("student");

            gridViewStudents.Columns[0].HeaderText = I18nUtil.getString("no");
            gridViewStudents.Columns[1].HeaderText = I18nUtil.getString("fullName");
            gridViewStudents.Columns[2].HeaderText = I18nUtil.getString("DTB");
            gridViewStudents.Columns[3].HeaderText = I18nUtil.getString("email");
            gridViewStudents.Columns[4].HeaderText = I18nUtil.getString("gender");
        }

        private void doAutoCompleteForTxtSearch()
        {
            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            DataTable table = StudentCntr.Instance.findAll();

            for (int i = 0; i < table.Rows.Count; i++)
            {
                auto.Add(table.Rows[i]["stu_fullname"].ToString());
            }

            txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtSearch.AutoCompleteCustomSource = auto;

        }

        private void bindGridViewStudents(String keyword)
        {
            gridViewStudents.Rows.Clear();
            DataTable table = StudentCntr.Instance.findBy(keyword);

            for (int i = 0; i < table.Rows.Count; i++)
            {
                gridViewStudents.Rows.Add(i + 1,
                                          table.Rows[i]["stu_fullname"].ToString(),
                                          table.Rows[i]["stu_dob"].ToString(),
                                          table.Rows[i]["stu_email"].ToString(),
                                          table.Rows[i]["gender"].ToString(),
                                          table.Rows[i]["stu_id"].ToString(),
                                          table.Rows[i]["stu_address"].ToString());
                gridViewStudents.Rows[gridViewStudents.RowCount - 1].Tag = table.Rows[i];
            }
            gridViewStudents.ClearSelection();
        }

        private void createStudentBtn_Click(object sender, EventArgs e)
        {
            String fullName = txtFullName.Text;
            DateTime dob = dobPicker.Value;
            String email = txtEmail.Text;
            bool gender = rdbtnMale.Checked ? true : false;
            String address = txtAddress.Text;

            bool result = StudentCntr.Instance.createStudent(fullName, dob, email, gender, address);
            
            if (result)
            {
                MessageBox.Show("Thêm học sinh thành công");
                bindGridViewStudents("");
                resetForm();
            }
        }

        private void gridViewStudents_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < gridViewStudents.SelectedRows.Count; i++)
            {
                studentId = Int32.Parse(gridViewStudents.SelectedRows[i].Cells[5].Value.ToString());
                ArrayList dataList = StudentCntr.Instance.findById(studentId);

                txtFullName.Text = dataList[0].ToString();
                dobPicker.Value = Convert.ToDateTime(dataList[1].ToString());
                txtEmail.Text = dataList[2].ToString();
                if(dataList[3].ToString().Equals("1")) rdbtnMale.Checked = true; else rdbtnFemale.Checked = true;
                txtAddress.Text = dataList[4].ToString();
                     
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            String fullName = txtFullName.Text;
            DateTime dob = Convert.ToDateTime(this.dobPicker.Value).Date;
            String email = txtEmail.Text;
            bool gender = rdbtnMale.Checked;
            String address = txtAddress.Text;

            if (StudentCntr.Instance.updateStudent(studentId, fullName, dob, email, gender, address))
            {
                MessageBox.Show("Cập nhật học sinh thành công");
                bindGridViewStudents("");
                resetForm();
            }

        }

        private void resetForm()
        {
            txtFullName.Text = "";
            txtEmail.Text = "";
            rdbtnMale.Checked = true;
            txtAddress.Text = "";
        }

        private void txtSearch_Enter(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            txtSearch.ForeColor = Color.Black;
        }

        private void txtSearch_Leave(object sender, EventArgs e)
        {
            txtSearch.Text = I18nUtil.getString("search");
            txtSearch.ForeColor = Color.Gray;
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                bindGridViewStudents(txtSearch.Text);
            }
        }

    }
}
